const tool = require('../tools/tool')
// const permissions = require('./permissions')
// const user = require('./user')
// const patrol = require('./patrol')
const db = require('../db')

const task = {
    list: [],
    running: false,
    async run() {
        if (this.running) return
        this.running = true
        while (1) {
            await tool.wait(0.01)
            let current = this.list[0]
            if (!current) continue
            await tool.sendSubscribeMessage(current.openId, current.content.length > 20 ? (current.content.slice(0, 19) + '…') : (current.content), current.title, tool.formatDate3(new Date()), current.username, current.data)
            // await tool.sendSubscribeMessage((await tool.methods.user().getSingleUserInfoByAccount(`2022400614`)).openId, current.content.length > 20 ? ('转发：' + current.content.slice(0, 19 - 3) + '…') : (current.content), current.title, tool.formatDate3(new Date()), current.username, current.data)
            this.shift()
        }
    },
    add(openId, content, title, username, data) {
        const handle = () => this.list.push({ openId, content, title, username, data })
        handle()
    },
    shift() {
        this.list.shift()
    }
}

task.run()

module.exports = {
    toOpenId(openId, content, title, username, data = {}) {
        task.add(openId, content, title, username, data)
    },
    toUser: {
        async byPhone(phone, content, title, username, data = {}) {
            let userData = await tool.methods.user().getSingleUserInfoByPhone(phone, true)
            if (!userData) throw '用户不存在'
            task.add(userData.openId, content, title, username, data)
        },
        async byUserId(userId, content, title, username, data = {}) {
            let userData = await tool.methods.user().getSingleUserInfoByUserId(userId, true)
            if (!userData) throw '用户不存在'
            task.add(userData.openId, content, title, username, data)
        },
        async byAccount(account, content, title, username, data = {}) {
            let userData = await tool.methods.user().getSingleUserInfoByAccount(account, true)
            if (!userData) throw '用户不存在'
            task.add(userData.openId, content, title, username, data)
        },
    },
    async toDepartment(departmentId, content, title, username, role) {
        let users = await db.selectWithParams(`select * from users where departmentId = ? ${role === undefined ? `` : `and role = ?`}`, [departmentId, (() => {
            if (role === undefined) return []
            return [role]
        })()])
        for (let i = 0; i < users.length; i++) {
            task.add(users[i].openId, content, title, username, data)
        }
    },
    async toHandleGroup(campusId, categoryId, content, title, username, data = {}) {
        let users = await db.selectWithParams(`select * from permissions where campusId = ? and categoryId = ? and type = 1`, [campusId, categoryId])
        for (let i = 0; i < users.length; i++) {
            task.add(users[i].openId, content, title, username, data)
        }
    },
    async toReviewGroup(campusId, categoryId, content, title, username, data = {}) {
        let users = await db.selectWithParams(`select * from permissions where campusId = ? and categoryId = ? and type = 2`, [campusId, categoryId])
        for (let i = 0; i < users.length; i++) {
            task.add(users[i].openId, content, title, username, data)
        }
    },
    async toRole(role, content, title, username, data = {}) {
        let users = await db.selectWithParams(`select * from users where role = ?`, [role])
        for (let i = 0; i < users.length; i++) {
            task.add(users[i].openId, content, title, username, data)
        }
    },
    async toPatrolCreater(patrolId, content, title, username, data = {}) {
        const patrol = require('./patrol')
        let patrolInfo = await tool.methods.patrol().getHead(patrolId)
        if (!patrolInfo) throw '巡查不存在'
        const user = require('./user')
        let userData = await tool.methods.user().getSingleUserInfoByUserId(patrolInfo.userId)
        if (!userData) throw '用户不存在'
        task.add(userData.openId, content, title, username, data)
    },
    async toAdmin(content, title, username, data = {}) {
        let users = await db.selectWithParams(`select * from users where isAdmin = 1`)
        for (let i = 0; i < users.length; i++) {
            task.add(users[i].openId, content, title, username, data)
        }
    }
}